from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..config.database import Base


class ChatMessage(Base):
    __tablename__ = "chat_messages"

    id = Column(Integer, primary_key=True, index=True)
    content = Column(Text, nullable=False)
    sender_type = Column(String, nullable=False)  # "user" 或 "ai"
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 外键关联到聊天室
    chat_room_id = Column(Integer, ForeignKey("chat_rooms.id"), nullable=False)
    
    # 与聊天室的多对一关系
    chat_room = relationship("ChatRoom", back_populates="messages")